package com.ruoyi.evalution.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.evalution.mapper.LabEvalutionMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.evalution.domain.LabEvalution;
import com.ruoyi.evalution.domain.LabEvalutionTarget;
import com.ruoyi.evalution.domain.LabEvalutionUser;
import com.ruoyi.evalution.service.ILabEvalutionService;
import com.ruoyi.evalution.service.ILabEvalutionTargetService;
import com.ruoyi.evalution.service.ILabEvalutionUserService;
import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.system.domain.ReceiptForm;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 实验室评测Service业务层处理
 * 
 * @author winner pan
 * @date 2022-04-15
 */
@Service
public class LabEvalutionServiceImpl implements ILabEvalutionService {
	@Autowired
	private LabEvalutionMapper labEvalutionMapper;
	@Autowired
	private ILabFileService labFileService;
	@Autowired
	private ILabEvalutionTargetService targetService;
	@Autowired
	private ILabEvalutionUserService userService;
	@Autowired
	private ILabFlowInstanceService labFlowInstanceService;
	
	private static final String belongType = FileTypeEnum.Evalution.getValue();

	/**
	 * 查询实验室评测
	 * 
	 * @param evalutionId
	 *            实验室评测ID
	 * @return 实验室评测
	 */
	@Override
	public LabEvalution selectLabEvalutionById(Long instanceId) {
		List<LabFile> labFiles = labFileService.getFiles(instanceId, belongType);
		LabEvalution labEvalution = labEvalutionMapper.selectLabEvalutionById(instanceId);
		labEvalution.setImages(labFiles);
		LabEvalutionUser labEvalutionUser = new LabEvalutionUser();
		labEvalutionUser.setEvalutionId(instanceId);
		labEvalution.setUserList(userService.selectLabEvalutionUserList(labEvalutionUser));
		LabEvalutionTarget labEvalutionTarget = new LabEvalutionTarget();
		labEvalutionTarget.setEvalutionId(instanceId);
		labEvalution.setTargetList(targetService.selectLabEvalutionTargetList(labEvalutionTarget));
		return labEvalution;
	}

	/**
	 * 查询实验室评测列表
	 * 
	 * @param labEvalution
	 *            实验室评测
	 * @return 实验室评测
	 */
	@Override
	public List<LabEvalution> selectLabEvalutionList(LabEvalution labEvalution) {
		return labEvalutionMapper.selectLabEvalutionList(labEvalution);
	}

	/**
	 * 新增实验室评测
	 * 
	 * @param labEvalution
	 *            实验室评测
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabEvalution(LabEvalution labEvalution) {
		labEvalution.setCreateBy(SecurityUtils.getUsername());
		labEvalution.setCreateOrgId(SecurityUtils.getLoginUser().getUser().getDeptId());
		labFlowInstanceService.insertLabFlowInstance(labEvalution);
		int ret = labEvalutionMapper.insertLabEvalution(labEvalution);
		labFileService.insertFiles(labEvalution.getImages(), labEvalution.getInstanceId(), belongType);
		labEvalution.getTargetList().forEach(item->{
			LabEvalutionTarget labEvalutionTarget = new LabEvalutionTarget();
			labEvalutionTarget.setEvalutionId(labEvalution.getInstanceId());
			labEvalutionTarget.setNickName(item.getNickName());
			labEvalutionTarget.setUserName(item.getUserName());
			this.targetService.insertLabEvalutionTarget(labEvalutionTarget);
		});
		labEvalution.getUserList().forEach(item ->{
			LabEvalutionUser labEvalutionUser = new LabEvalutionUser();
			labEvalutionUser.setEvalutionId(labEvalution.getInstanceId());
			labEvalutionUser.setNickName(item.getNickName());
			labEvalutionUser.setUserName(item.getUserName());
			this.userService.insertLabEvalutionUser(labEvalutionUser);
		});
		return ret;
	}

	/**
	 * 修改实验室评测
	 * 
	 * @param labEvalution
	 *            实验室评测
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabEvalution(LabEvalution labEvalution) {
		labFileService.updateFiles(labEvalution.getImages(), labEvalution.getInstanceId(), belongType);
		this.userService.deleteLabEvalutionUserByEvalution(labEvalution.getInstanceId());
		this.targetService.deleteLabEvalutionTargetByEvalution(labEvalution.getInstanceId());
		labEvalution.getTargetList().forEach(item->{
			LabEvalutionTarget labEvalutionTarget = new LabEvalutionTarget();
			labEvalutionTarget.setEvalutionId(labEvalution.getInstanceId());
			labEvalutionTarget.setNickName(item.getNickName());
			labEvalutionTarget.setUserName(item.getUserName());
			this.targetService.insertLabEvalutionTarget(labEvalutionTarget);
		});
		labEvalution.getUserList().forEach(item ->{
			LabEvalutionUser labEvalutionUser = new LabEvalutionUser();
			labEvalutionUser.setEvalutionId(labEvalution.getInstanceId());
			labEvalutionUser.setNickName(item.getNickName());
			labEvalutionUser.setUserName(item.getUserName());
			this.userService.insertLabEvalutionUser(labEvalutionUser);
		});
		labFlowInstanceService.updateLabFlowInstance(labEvalution);
		return labEvalutionMapper.updateLabEvalution(labEvalution);
	}

	/**
	 * 批量删除实验室评测
	 * 
	 * @param evalutionIds
	 *            需要删除的实验室评测ID
	 * @return 结果
	 */
/*	@Override
	public int deleteLabEvalutionByIds(Long[] evalutionIds) {
		return labEvalutionMapper.deleteLabEvalutionByIds(evalutionIds);
	}*/

	/**
	 * 删除实验室评测信息
	 * 
	 * @param evalutionId
	 *            实验室评测ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabEvalutionById(Long instancetIds) {
		labFlowInstanceService.deleteLabFlowInstanceById(instancetIds);
		labFileService.deleteLabFile(instancetIds, belongType);
		return labEvalutionMapper.deleteLabEvalutionById(instancetIds);
	}
	
	@Override
	@Transactional
	public int commitLabEvalution(LabEvalution labEvalution) {
		this.updateLabEvalution(labEvalution);
		return labFlowInstanceService.commitFlow(labEvalution);
	}
	
	public int updateLabEvalutionStatus(String status){
		return labEvalutionMapper.updateLabEvalutionStatus(status);
	}
}
